package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishFlavorMapper;
import com.sky.mapper.DishMapper;
import com.sky.service.DishService;
import com.sky.vo.DishPageVo;
import com.sky.vo.DishVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service

public class DishServiceImpl implements DishService {

    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private DishFlavorMapper dishFlavorMapper;

    @Override
    @Transactional
    public void insert(DishDTO dto) {
        Dish dish = new Dish();

        BeanUtils.copyProperties(dto, dish);

        dishMapper.insertDish(dish);

        long dishId = dish.getId();

        List<DishFlavor> flavors = dto.getFlavors();
        System.out.println(flavors);
        if (dto.getFlavors() != null && dto.getFlavors().size() > 0) {
            dto.getFlavors().forEach(s -> {
                s.setDishId(dishId);

            });
        }

        dishFlavorMapper.insertFlavo(dto.getFlavors());
    }

    /**
     * 根据分类id查询菜品
     * */
    @Override
    public List<Dish> selectByCategoryId(Long categoryId) {
        List<Dish> dish = dishMapper.selectByCategoryId(categoryId);
        return dish;
    }

    /**菜品分页查询*/
    @Override
    public DishPageVo selectByPage(DishPageQueryDTO queryDTO) {
        PageHelper.startPage(queryDTO.getPage(),queryDTO.getPageSize());

        Page<DishVO> dishVOS = dishMapper.selectByPage(queryDTO.getCategoryId(),queryDTO.getName(),queryDTO.getStatus());

        DishPageVo dishVO = new DishPageVo();

        dishVO.setTotal(dishVOS.getTotal());

        dishVO.setRecords(dishVOS.getResult());

        return dishVO;
    }


    /**删除菜品*/
    @Override
    @Transactional
    public void deleteDish(List ids) {

        //起售状态不能删除
        List listStatus = dishMapper.selectByIdsInDish(ids);//查询要删除菜品的状态
        if(listStatus != null && listStatus.size() != 0){
              throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
        }

        //被套餐关联的菜品不等你删除
        List list = dishMapper.selectByIdsInS_D(ids);
        if(list != null && list.size() != 0){
            throw new DeletionNotAllowedException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);
        }
        //删除菜品数据
        dishMapper.deleteDishByIds(ids);
        //删除口味数据
        dishFlavorMapper.deleteByFlavorIds(ids);
    }
/**更新菜品数据*/
    @Override
    @Transactional
    public void updateDish(DishDTO dto) {
        Dish dish = new Dish();

        BeanUtils.copyProperties(dto,dish);

        dishMapper.updateDish(dish);

        List<DishFlavor> flavors = dto.getFlavors();

        if(flavors != null && flavors.size() != 0){

            dishFlavorMapper.deleteflavors(dish.getId());

            flavors.forEach(s->{
                s.setDishId(dish.getId());
            });

            dishFlavorMapper.insertFlavo(flavors);
        }
    }
/**修改启售/禁售状态*/
    @Override
    public void updateStatus(Integer status,long id) {
        dishMapper.updateStatus(status,id);
    }
/**根据id查询*/
    @Override
    @Transactional
    public DishVO selectById(Long id) {
        DishVO dishVO = new DishVO();

        DishVO dishVO1 = dishMapper.selectById(id);

        BeanUtils.copyProperties(dishVO1,dishVO);

        List<DishFlavor> flavor = dishFlavorMapper.deleteByFlavorId(dishVO1.getId());

        dishVO1.setFlavors(flavor);
        return dishVO1;
    }
}
